package com.chinatsp.onlineapp.ui.violation;

import java.util.ArrayList;
import java.util.List;

import tsp.cn.http.tools.PreferenceUtils;

import android.content.Context;
import android.preference.Preference;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.chinatsp.onlineapp.R;
import com.chinatsp.onlineapp.base.Constant;
import com.chinatsp.onlineapp.bean.CityList;

/**
 * 城市列表 适配器
 */

public class CityListAdapter extends BaseAdapter {
	private Context mContext;
	private List<CityList> cityLists;
	private String defaultCityName="北京";
	public String selectCity = "北京";
	private int selectposition = 0;
	

	public CityListAdapter(Context context, List<CityList> cityLists) {
		mContext = context;
		this.cityLists = cityLists;
		defaultCityName = PreferenceUtils.getPreference(Constant.DEFAULT_CITY, defaultCityName);
		selectCity = PreferenceUtils.getPreference(Constant.SELECT_CITY, selectCity);
	}

	@Override
	public int getCount() {
		return cityLists == null ? 0 : cityLists.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.layout_city_list_item, parent, false);
			holder = new ViewHolder();
			holder.vg_city = (ViewGroup) convertView;
			holder.cbxIsSelect = (CheckBox) convertView
					.findViewById(R.id.cbx_city);
			holder.tv_city = (TextView) convertView.findViewById(R.id.tv_city);
			holder.iv_location = (ImageView) convertView.findViewById(R.id.iv_location);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		if (cityLists.get(position).getName().contains(defaultCityName)) {
			holder.iv_location.setVisibility(View.VISIBLE);
		}
		
		
		if (cityLists.get(position).getName().contains(selectCity)) {
			holder.cbxIsSelect.setSelected(true);
		}else{
			holder.cbxIsSelect.setSelected(false);
		}
	
	
		holder.tv_city.setText(cityLists.get(position).getName());
		
		holder.vg_city.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
			selectCity = ((TextView)((ViewGroup)view).getChildAt(1)).getText().toString();
			selectposition = position;
			PreferenceUtils.savePreference(Constant.SELECT_CITY, selectCity);
			notifyDataSetChanged();
				
			}
		});
		
	

		return convertView;
	}
	
	



	static class ViewHolder {
		ViewGroup vg_city;
		CheckBox cbxIsSelect;
		TextView tv_city;
		ImageView iv_location;
	}

}
